/**
 * 
 */
init();

function init(){
	getDocTree();
}

function getDocTree(){
	
	var mydata=[
		{
		"fileId": 2,
		"fileName": "公共文档",
		"fileType": "1",
		"remark": "对整个系统",
		"fileOwner": "黄永",
		"carentId": 0,
		"filePath": "D:/",
		"ifDelete": 0,
		"children": []
	}, 
		{
		"fileId": 3,
		"fileName": "管理制度",
		"fileType": "1",
		"remark": "公司管理规范",
		"fileOwner": "黄永",
		"carentId": 0,
		"filePath": "D:/",
		"ifDelete": 0,
		"children": []
	}, 
		{
		"fileId": 4,
		"fileName": "部门文档",
		"fileType": "1",
		"remark": "各部门文件分类",
		"fileOwner": "黄永",
		"carentId": 0,
		"filePath": "D:/",
		"ifDelete": 0,
		"children": [
			{
			"fileId": 5,
			"fileName": "研发部",
			"fileType": "1",
			"remark": "研发部文档",
			"fileOwner": "黄永",
			"carentId": 4,
			"filePath": "D:/",
			"ifDelete": 0,
			"children": [
				{
				"fileId": 6,
				"fileName": "所有项目",
				"fileType": "1",
				"remark": "所有可用项目",
				"fileOwner": "黄永",
				"carentId": 5,
				"filePath": "D:/",
				"ifDelete": 0,
				"children": []
				}, 
				{
				"fileId": 7,
				"fileName": "部门员工介绍",
				"fileType": "1",
				"remark": "介绍所有研发部员工",
				"fileOwner": "黄永",
				"carentId": 5,
				"filePath": "D:/",
				"ifDelete": 0,
				"children": [
					{
					"fileId": 8,
					"fileName": "员工基本资料",
					"fileType": "3",
					"remark": "详细资料",
					"fileOwner": "黄永",
					"carentId": 7,
					"filePath": "D:/",
					"ifDelete": 0,
					"children": []
					},
					]
				},
			]
		}, 
			{
			"fileId": 10,
			"fileName": "OA文档",
			"fileType": "1",
			"remark": "毕设项目",
			"fileOwner": "黄永",
			"carentId": 4,
			"filePath": "D:/",
			"ifDelete": 0,
			"children": [
				{
				"fileId": 9,
				"fileName": "OA系统说明文档",
				"fileType": "3",
				"remark": "说明文档",
				"fileOwner": "黄永",
				"carentId": 10,
				"filePath": "D:/",
				"ifDelete": 0,
				"children": []
				},
			]
		},
		]
	},
	]
	
	/*递归实现获取无级树数据并生成DOM结构*/
	var str = "";
	var forTree = function(o) {
		for (var i = 0; i < o.length; i++) {
			var urlstr = "";
			try {
				if (typeof o[i]["carentId"] == "0") {
					urlstr = "<div><span>" + o[i]["fileName"] + "</span><ul>";
				} else {
					urlstr = "<div><span><a href=" + o[i]["filePath"] + ">" + o[i]["fileName"] + "</a></span><ul>";
				}
				str += urlstr;
				if (o[i]["children"] != null) {
					forTree(o[i]["children"]);
				}
				str += "</ul></div>";
			} catch (e) {}
		}
		return str;
	}

	/*添加无级树*/
	document.getElementById("menuTree").innerHTML = forTree(mydata);

	/*树形菜单*/
	var menuTree = function() {
		//给有子对象的元素加[+-]
		$("#menuTree ul").each(function(index, element) {
			var ulContent = $(element).html();
			var spanContent = $(element).siblings("span").html();
			if (ulContent) {
				$(element).siblings("span").html("[+] " + spanContent)
			}
		});

		$("#menuTree").find("div span").click(function() {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				if (ul.css("display") == "none") {
					ul.show(300);
					$(this).html("[-] " + spanContent);
				} else {
					ul.hide(300);
					$(this).html("[+] " + spanContent);
				}
			}
		});
	}()

	/*展开*/
	$("#btn_open").click(function() {
		$("#menuTree ul").show(300);
		curzt("-");
	})
	/*收缩*/
	$("#btn_close").click(function() {
		$("#menuTree ul").hide(300);
		curzt("+");
	})

	function curzt(v) {
		$("#menuTree span").each(function(index, element) {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				$(this).html("[" + v + "] " + spanContent);
			}
		});
	}

	// 公共文档(文件夹)
	// 管理制度(文件夹)
	// 部门文档(文件夹)
	// 	OA文档
	// 		OA系统说明文档
	// 	研发部(文件夹)
	// 		所有项目(文件夹)
	// 		部门员工介绍
	// 			员工基本资料
}